package com.woniuxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.mapper.SysRoleUserMapper;
import com.woniuxy.mapper.UserMapper;
import com.woniuxy.pojo.SysRoleUser;
import com.woniuxy.pojo.User;
import com.woniuxy.pojo.vo.RoleUserVO;
import com.woniuxy.service.SysRoleUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author Administrator
* @description 针对表【t_sys_role_user】的数据库操作Service实现
* @createDate 2025-06-05 12:08:03
*/
@Service
public class SysRoleUserServiceImpl extends ServiceImpl<SysRoleUserMapper, SysRoleUser>
    implements SysRoleUserService {
    @Autowired
    SysRoleUserMapper sysRoleUserMapper;
    @Autowired
    UserMapper userMapper;
    @Override
    public List<RoleUserVO> getRoleUser(Integer roleId) {
        // 1. 根据角色ID查询关联的用户ID
        LambdaQueryWrapper<SysRoleUser> wrapper = Wrappers.<SysRoleUser>lambdaQuery()
                .eq(SysRoleUser::getRoleId, roleId);
        List<SysRoleUser> roleUsers = sysRoleUserMapper.selectList(wrapper);

        if (CollectionUtils.isEmpty(roleUsers)) {
            return Collections.emptyList();
        }

        // 2. 提取用户ID列表
        List<Integer> userIds = roleUsers.stream()
                .map(SysRoleUser::getUserId)
                .collect(Collectors.toList());

        // 3. 查询用户信息
        List<User> users = userMapper.selectList(
                Wrappers.<User>lambdaQuery().in(User::getId, userIds));

        // 4. 转换为VO列表
        return users.stream()
                .map(user -> {
                    RoleUserVO vo = new RoleUserVO();
                    BeanUtils.copyProperties(user, vo);
                    return vo;
                })
                .collect(Collectors.toList());
    }
}




